package com.example.administrator.easy.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.easy.R;
import com.example.administrator.easy.model.GuangZbResult;

import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2016/11/15.
 */

public class GuangZbLvAdapter extends BaseAdapter {//
    private Context mContext;
    private ArrayList<GuangZbResult> mList;
    public GuangZbLvAdapter(Context context,ArrayList<GuangZbResult> list){
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if(convertView==null){
            convertView = View.inflate(mContext,R.layout.fragment_guang_zb_item,null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }

        if(position==0){
            vh.tv.setVisibility(View.VISIBLE);
        }else{
            vh.tv.setVisibility(View.GONE);
        }

        String now = mList.get(position).getEventsEnd();
        String d= now.substring(6,19);
        long dd = Long.parseLong(d);
        Log.e("Maind",d+"");
        //把long型的时间转为年月日形式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM--dd HH:mm");
        Long in = Long.parseLong(d);
        Date date= new Date(in);
        Log.e("Maindate",sdf.format(date)+"");
        vh.tvtime.setText(sdf.format(date));
        if(System.currentTimeMillis()>dd){//当前时间大于截止时间，则表示已结束
            vh.tvtime.setBackgroundResource(R.mipmap.large_gray);//背景为灰色
            vh.tvcount.setText(mList.get(position).getChatPeopleCount()+"");
        }else {
            vh.tvtime.setBackgroundResource(R.mipmap.read_live);//背景为红色
        }
        vh.tvperson.setText(mList.get(position).getStarName());
        vh.tvtype.setText(mList.get(position).getTitle());
        x.image().bind(vh.iv,mList.get(position).getIco());
        return convertView;
    }

    class ViewHolder{
        TextView tv,tvtime,tvperson,tvtype,tvcount;
        ImageView iv;
        public ViewHolder(View view){
            tv = (TextView) view.findViewById(R.id.tv);
            tvtime = (TextView) view.findViewById(R.id.tvtime);
            tvperson = (TextView) view.findViewById(R.id.tvperson);
            tvtype = (TextView) view.findViewById(R.id.tvtype);
            tvcount = (TextView) view.findViewById(R.id.tvcount);
            iv = (ImageView) view.findViewById(R.id.zbiv);
        }
    }
}
